package me.jessyan.armscomponent.app.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.di.component.DaggerWelComeComponent;
import me.jessyan.armscomponent.app.mvp.contract.WelComeContract;
import me.jessyan.armscomponent.app.mvp.presenter.WelComePresenter;
import me.jessyan.armscomponent.app.mvp.ui.adapter.WelcomeViewPagerAdapter;
import me.jessyan.armscomponent.app.mvp.ui.widget.ScaleCircleNavigator;
import me.jessyan.armscomponent.commonsdk.core.AccountDataProvider;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * <p>
 * Author: Taozebi
 * Date: 09/26/2021 14:55
 * Description: describe of this class
 */
@Route(path = RouterHub.APP_WELCOMEACTIVITY)
public class WelcomeActivity extends BaseActivity<WelComePresenter> implements WelComeContract.View {

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerWelComeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    private static final Integer[] CHANNELS = new Integer[]{R.drawable.public_bg_welcome_01,R.drawable.public_bg_welcome_02,R.drawable.public_bg_welcome_03};

    @BindView(R.id.view_pager)
    public ViewPager mViewPager;

    @BindView(R.id.magic_indicator)
    public MagicIndicator mMagicIndicator;

    @BindView(R.id.tv_experience_it_now)
    public TextView tv_experience_it_now;
    @BindView(R.id.tv_skip)
    public TextView tv_skip;


    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_welcome; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initMagicIndicator1();
    }

    @OnClick({R.id.tv_experience_it_now,R.id.tv_skip})
    public void onClicked(View view){
        switch (view.getId()){
            case R.id.tv_experience_it_now:
            case R.id.tv_skip:
//                ProjectUtils.deleteLocalDir();
//                if(AccountDataProvider.getInstance(WelcomeActivity.this).isAutomaticLogon()){
//                    Utils.navigation(WelcomeActivity.this, RouterHub.APP_MAINACTIVITY);
//                    finish();
//                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//                }else{
                    Utils.navigation(WelcomeActivity.this, RouterHub.USER_LOGINACTIVITY);
                    finish();
                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
//                }
//                finish();
                break;
        }
    }


    private void initMagicIndicator1() {
        List<View> views = new ArrayList<View>();
        LayoutInflater inflater=getLayoutInflater();
        View view1 =inflater.inflate(R.layout.welcome_lay1, null);
        View view2 =inflater.inflate(R.layout.welcome_lay2, null);
        View view3 =inflater.inflate(R.layout.welcome_lay3, null);
        views.add(view1);
        views.add(view2);
        views.add(view3);
        mViewPager.setAdapter(new WelcomeViewPagerAdapter(views));

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(position == 2){
                    tv_skip.setVisibility(View.INVISIBLE);
                    tv_experience_it_now.setVisibility(View.VISIBLE);
                }else{
                    tv_experience_it_now.setVisibility(View.INVISIBLE);
                    tv_skip.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        ScaleCircleNavigator scaleCircleNavigator = new ScaleCircleNavigator(this);
        scaleCircleNavigator.setCircleCount(CHANNELS.length);
        scaleCircleNavigator.setNormalCircleColor(Color.WHITE);
        scaleCircleNavigator.setSelectedCircleColor(Color.parseColor("#63B350"));
        scaleCircleNavigator.setCircleClickListener(index -> mViewPager.setCurrentItem(index));
        mMagicIndicator.setNavigator(scaleCircleNavigator);
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }
}
